package com.yuqiang.homePage.controller;

import com.yuqiang.homePage.model.vo.SessionVo;
import com.yuqiang.user.model.po.KeyPo;
import com.yuqiang.user.model.vo.KeyVo;
import com.yuqiang.user.service.KeyService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yuqiang.common.model.ResultModel;
import com.yuqiang.common.util.BusinessConstant;
import com.yuqiang.homePage.model.bo.HomePageBo;
import com.yuqiang.homePage.service.HomePageService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/BM/")
public class HomePageController {

	@Autowired
	private HomePageService homePageService;

	@Autowired
	private KeyService keyService;


	//主页初始化
	@RequestMapping("index")
	public ModelAndView initIndex() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/index/index");
		return modelAndView;
	}

	//关于页面初始化
	@RequestMapping("about")
	public ModelAndView initAbout() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/index/about");
		return modelAndView;
	}

	//登录页面初始化
	@RequestMapping("login")
	public ModelAndView initLogin(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
//		if(request.getSession().getAttribute("userName")!=null){
//			if(Integer.parseInt(request.getSession().getAttribute("userType").toString())==999){
//				modelAndView.setViewName("/userInfo/initUserInfo");
//			}else{
//				modelAndView.setViewName("/userInfo/initUserPersonal");
//			}
//			return modelAndView;
//		}
		modelAndView.setViewName("/index/login");
		return modelAndView;
	}

	//注册页面初始化
	@RequestMapping("register")
	public ModelAndView initRegister() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/index/register");
		return modelAndView;
	}

	//注册验证
	@RequestMapping("checkRegister")
	@ResponseBody
	public synchronized ResultModel checkRegister(HomePageBo homePageBo)
			throws InterruptedException {
		ResultModel resultModel = new ResultModel();
		//防呆
		if (StringUtils.isBlank(StringUtils.trim(homePageBo.getUserName()))) {
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.USERNAME_IS_BLANK);
			return resultModel;
		}
		if (StringUtils.isBlank(StringUtils.trim(homePageBo.getPassword()))) {
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.PASSWORD_IS_BLANK);
			return resultModel;
		}
		if (StringUtils.isBlank(StringUtils.trim(homePageBo.getKey()))) {
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.KEY_IS_BLANK);
			return resultModel;
		}
		//先检查key的正确性
		resultModel = homePageService.checkKey(homePageBo);
		KeyVo keyVo = resultModel.getKeyVo();
		if (!resultModel.getSuccess()) {
			return resultModel;
		}
		//再检查用户名的唯一性
		resultModel = homePageService.checkUserName(homePageBo);
		if (!resultModel.getSuccess()) {
			return resultModel;
		}
		homePageBo.setUserType(keyVo.getkType());
		//修改key的使用状态
		KeyPo po = new KeyPo();
		po.setkId(keyVo.getkId());
		po.setKIsused("T");
		keyService.updateKeyById(po);
		//注册
		resultModel = homePageService.registerUser(homePageBo);

		return resultModel;
	}

	//登录验证
	@RequestMapping("checkLogin")
	@ResponseBody
	public ResultModel checkLogin(HomePageBo homePageBo,HttpServletRequest request) {
		ResultModel resultModel = new ResultModel();
		//防呆
		if (StringUtils.isBlank(StringUtils.trim(homePageBo.getUserName()))) {
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.USERNAME_IS_BLANK);
			return resultModel;
		}
		if (StringUtils.isBlank(StringUtils.trim(homePageBo.getPassword()))) {
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.PASSWORD_IS_BLANK);
			return resultModel;
		}
		//检查是否重复登陆
		if(request.getSession().getAttribute("userName")!=null){
			resultModel.setSuccess(false);
			resultModel.setMessage("你已经登录，请先退出登录！");
			return resultModel;
		}
		//检查用户名密码是否正确
		resultModel = homePageService.checkLogin(homePageBo);
		if(resultModel.getSuccess()){
			List<SessionVo> list = homePageService.getSession(homePageBo.getUserName());
			if(CollectionUtils.isNotEmpty(list)){
				//将登陆信息存储到session
				SessionVo sessionVo = list.get(0);
				HttpSession session = request.getSession();
				session.setAttribute("userId", sessionVo.getUserId());
				session.setAttribute("userName", sessionVo.getUserName());
				session.setAttribute("userType", sessionVo.getUserType());
				resultModel.setUserType(sessionVo.getUserType());
			}else{
				resultModel.setSuccess(false);
				resultModel.setMessage("用户名不存在！");
			}
		}
		return resultModel;
	}

	//退出登录
	@RequestMapping("overLogin")
	public ModelAndView overLogin(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.removeAttribute("userId");
		session.removeAttribute("userName");
		session.removeAttribute("userType");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/index/login");
		return modelAndView;
	}

	//管理页面
	@RequestMapping("manage")
	public ModelAndView manage(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		if(request.getSession().getAttribute("userName")==null){
			modelAndView.setViewName("/index/login");
			return modelAndView;
		}
		if( Integer.parseInt(request.getSession().getAttribute("userType").toString())!=999){
			modelAndView.setViewName("/index/personal");
			return modelAndView;
		}
		modelAndView.setViewName("/index/manage");
		return modelAndView;
	}

	//个人页面
	@RequestMapping("personal")
	public ModelAndView personal(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView();
		if(request.getSession().getAttribute("userName")==null){
			modelAndView.setViewName("/index/login");
			return modelAndView;
		}
		if("999".equals(request.getSession().getAttribute("userType").toString())){
			modelAndView.setViewName("/index/manage");
			return modelAndView;
		}
		modelAndView.setViewName("/index/personal");
		return modelAndView;
	}

}
